﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using saigonbook.Dal;

namespace saigonbook.Biz
{
    public class BIZ_tonkho
    {
        public DateTime ngay;
        public BIZ_sach sach;
        public int soluong;
        public DateTime Ngay
        {
            get { return ngay; }
            set { ngay = value; }
        }
        public BIZ_sach Sach
        {
            get { return sach; }
            set { sach = value; }
        }

        public string Tensach
        {
            get { return Sach == null ? String.Empty : Sach.Tensach; }
        }

        public int Soluong
        {
            get { return soluong; }
            set { soluong = value; }
        }
        public BIZ_tonkho()
        { }
        public BIZ_tonkho(DateTime ngay, BIZ_sach sach, int soluong)
        {
            this.ngay = ngay;
            this.sach = sach;
            this.soluong = soluong;
        }

        public List<BIZ_tonkho> Select_tonkho()
        {
            return DAL_tonkho.Select_tonkho();
        }

        public List<BIZ_tonkho> Select_tonkho(DateTime ngay)
        {
            List<BIZ_sach> dssach = new List<BIZ_sach>();
            List<BIZ_tonkho> dstonkho = new List<BIZ_tonkho>();
            dssach = DAL_sach.Select_sach();
            int i = 1;
            foreach(BIZ_sach sach in dssach)
            {
                BIZ_tonkho tk = new BIZ_tonkho();
                tk.sach = new BIZ_sach();
                tk.ngay = ngay.Date;
                tk.sach = sach;
                tk.soluong = soluongtonkho(tk);
                dstonkho.Add(tk);
                if (i == dssach.Count)
                    break;
                i++;
            }
            return dstonkho;
        }

        public List<BIZ_tonkho> Select_tonkho(DateTime ngay, int maloai)
        {
            List<BIZ_sach> dssach = new List<BIZ_sach>();
            List<BIZ_tonkho> dstonkho = new List<BIZ_tonkho>();
            dssach = DAL_sach.Select_sach(maloai);
            int i = 1;
            foreach (BIZ_sach sach in dssach)
            {
                BIZ_tonkho tk = new BIZ_tonkho();
                tk.sach = new BIZ_sach();
                tk.ngay = ngay.Date;
                tk.sach = sach;
                tk.soluong = soluongtonkho(tk);
                dstonkho.Add(tk);
                if (i == dssach.Count)
                    break;
                i++;
            }
            return dstonkho;
        }

        public bool kiemtratontaingay(BIZ_tonkho tonkho)
        {
            List<BIZ_tonkho> ds = new List<BIZ_tonkho>();
            ds = DAL_tonkho.Select_tonkho();
            foreach (BIZ_tonkho tk in ds)
                if (tk.ngay.Date == tonkho.ngay.Date && tk.sach.masach == tonkho.sach.masach)
                    return true;
            return false;
        }

        public int soluongtonkho(BIZ_tonkho tonkho)
        {
            int soluong = 0;
            List<BIZ_tonkho> ds = new List<BIZ_tonkho>();
            ds = DAL_tonkho.Select_tonkho(tonkho.sach);
            ds = (from tk in ds where tk.sach.masach == tonkho.sach.masach && tk.ngay.Date <= tonkho.ngay.Date orderby tk.ngay descending select tk).Take(1).ToList();
            foreach (BIZ_tonkho _tk in ds)
            {
                soluong = (int)_tk.soluong;
                break;
            } 
            return soluong;
        } 
    }
}
